package com.example.yubo.zyfypt_403yb.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yubo.zyfypt_403yb.R;
import com.example.yubo.zyfypt_403yb.bean.LoginBean;
import com.example.yubo.zyfypt_403yb.iface.LoginListener;
import com.example.yubo.zyfypt_403yb.model.LoginModel;


public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    //private TextView textViewname,textViewpassword;
    //1
    private TextView tv1,tvname,tvwe,tvlogin;

    private EditText ed_name, ed_password;
    private Button bt_login, bt_register;
    private Switch sw;

    private String username = "", password = "",sessionid = "";//用户名密码
    private SharedPreferences sp;
    private LoginListener loginListener = new LoginListener() {
        @Override
        public void onResponse(LoginBean loginBean) {
            System.out.println("result:"+loginBean.getId());
            if(loginBean.getSessionid()!=null) {
                sessionid = loginBean.getSessionid().toString();
                username = loginBean.getUsername();
                saveSP();
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
            }
        }

        @Override
        public void onFail(String msg) {
            System.out.println("fail");
        }
    };
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Toast.makeText(this,"登录",Toast.LENGTH_SHORT).show();
        init();
        //实例化sp
        //实例化sp
        sp=getSharedPreferences("login",MODE_PRIVATE);
        //读取sp
        readSP();
    }
    private void readSP() {
        String name=sp.getString("username",null);
        String pass1=sp.getString("password",null);
        String sessionid=sp.getString("sessionid",null);
        Boolean b=sp.getBoolean("remember",false);
        if(b){
            ed_name.setText(name);
            ed_password.setText(pass1);
            sw.setChecked(b);
        }
    }


    private void init() {
         tv1= (TextView) findViewById(R.id.textView);
         tvname= (TextView) findViewById(R.id.textView2);
        //2
        ed_name = (EditText) findViewById(R.id.editText);
        ed_password = (EditText) findViewById(R.id.editText2);
        bt_login = (Button) findViewById(R.id.button);
        bt_register = (Button) findViewById(R.id.button4);
        //3
        bt_login.setOnClickListener(this);
        bt_register.setOnClickListener(this);
        sw=(Switch) findViewById(R.id.switch6);

    }
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                //Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                //startActivity(intent);
                LoginModel loginModel = new LoginModel();
                loginModel.getResult(ed_name.getText().toString(), ed_password.getText().toString(), loginListener);
                saveSP();
                break;
            case R.id.button4:
                // Intent intent2 = new Intent(LoginActivity.this,RegisterActivity.class);
                // startActivity(intent2);
                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);//forward为当前activity，forwardTarget为新的activity
                startActivity(intent);
                break;
            default:
                break;
        }
    }
    private void saveSP() {
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("username", username);
        editor.putString("password", password);
        editor.putString("sessionid", sessionid);
        editor.putBoolean("remember", sw.isChecked());
        editor.commit();//提交保存
    }
}


